/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "../../stdafx.h"
#include "../../include/date/HeadDiv.h"

namespace FaceCat{
	HeadDiv::HeadDiv(FCCalendar *calendar){
		m_calendar = calendar;
		m_dateTitle = 0;
		m_lastBtn = 0;
		m_nextBtn = 0;
        m_weekStrings[0] = L"Sun";
        m_weekStrings[1] = L"Mon";
        m_weekStrings[2] = L"Tues";
        m_weekStrings[3] = L"Wed";
        m_weekStrings[4] = L"Thur";
        m_weekStrings[5] = L"Fri";
        m_weekStrings[6] = L"Sat";
		FCFont font(L"Default", 14, true, false, false);
		setFont(&font);
		setHeight(55);
	}

	HeadDiv::~HeadDiv(){
		m_calendar = 0;
		m_dateTitle = 0;
		m_lastBtn = 0;
		m_nextBtn = 0;
	}

	FCCalendar* HeadDiv::getCalendar(){
		return m_calendar;
	}

	void HeadDiv::setCalendar(FCCalendar *value){
		m_calendar = value;
	}

	DateTitle* HeadDiv::getDateTitle(){
		return m_dateTitle;
	}

	void HeadDiv::setDateTitle(DateTitle *value){
		m_dateTitle = value;
	}

	ArrowButton* HeadDiv::getLastBtn(){
		return m_lastBtn;
	}

	void HeadDiv::setLastBtn(ArrowButton *value){
		m_lastBtn = value;
	}

	ArrowButton* HeadDiv::getNextBtn(){
		return m_nextBtn;
	}

	void HeadDiv::setNextBtn(ArrowButton *value){
		m_nextBtn = value;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	String HeadDiv::getViewType(){
		return L"HeadDiv";
	}

	void HeadDiv::onLoad(){
		FCHost *host = getNative()->getHost();
		if(!m_dateTitle){
			m_dateTitle = m_calendar->createDateTitle();
			addView(m_dateTitle);
		}
		if(!m_lastBtn){
			m_lastBtn = m_calendar->createArrowButton();
			addView(m_lastBtn);
		}
		if(!m_nextBtn){
			m_nextBtn = m_calendar->createArrowButton();
			m_nextBtn->setToLast(false);
			addView(m_nextBtn);
		}
	}

	void HeadDiv::onPaintBackground(FCPaint *paint, const FCRect& clipRect){
		int width = getWidth(), height = getHeight();
		FCRect rect = {0, 0, width, height};
		paint->fillRect(getPaintingBackColor(), rect);         	
	}

	void HeadDiv::onPaintBorder(FCPaint *paint, const FCRect& clipRect){
		int width = getWidth(), height = getHeight();
		paint->drawLine(getPaintingBorderColor(), 1, 0, 0, height, width, height);
	}

	void HeadDiv::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
		int width = getWidth(), height = getHeight();
		FCCalendarMode mode = m_calendar->getMode();
		if(mode == FCCalendarMode_Day){
			int left = 0;
			FCSize weekDaySize = {0};
			FCFont *font = getFont();
			Long textColor = getPaintingTextColor();
			for(int i = 0;i < 7; i++){
				weekDaySize = paint->textSize(m_weekStrings[i], font, -1);
				int textX = left + (width / 7) / 2 - weekDaySize.cx / 2;
				int textY = height - weekDaySize.cy - 2;
				FCRect weekRect = {textX, textY, textX + weekDaySize.cx, textY + weekDaySize.cy};
				paint->drawText(m_weekStrings[i], textColor, font, weekRect, -1);
				left += width / 7;
			}
		}
	}

	void HeadDiv::update(){
		FCView::update();
        int width = getWidth(), height = getHeight();
		if (m_dateTitle){
			FCPoint location = {(width - m_dateTitle->getWidth()) / 2, (height - m_dateTitle->getHeight()) / 2};
            m_dateTitle->setLocation(location);
        }
        if (m_lastBtn){
			FCPoint location = {2, (height - m_lastBtn->getHeight()) / 2};
            m_lastBtn->setLocation(location);
        }
        if (m_nextBtn){
			FCPoint location = {width - m_nextBtn->getWidth() - 2, (height - m_nextBtn->getHeight()) / 2};
            m_nextBtn->setLocation(location);
        }
	}
}
